package org.jeecg.modules.phone.service.impl;

import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.util.StringUtil;
import org.jeecg.modules.phone.entity.PhoneEntity;
import org.jeecg.modules.phone.entity.PhoneEntityTaskRsource;
import org.jeecg.modules.phone.entity.PhoneEntityTaskRsourceVo;
import org.jeecg.modules.phone.enums.EntityEnums;
import org.jeecg.modules.phone.mapper.PhoneEntityTaskRsourceMapper;
import org.jeecg.modules.phone.service.IPhoneEntityTaskRsourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 资源管理
 * @Author: jeecg-boot
 * @Date:   2022-08-31
 * @Version: V1.0
 */
@Service
public class PhoneEntityTaskRsourceServiceImpl extends ServiceImpl<PhoneEntityTaskRsourceMapper, PhoneEntityTaskRsource> implements IPhoneEntityTaskRsourceService {
    @Autowired
    PhoneEntityTaskRsourceMapper phoneEntityTaskRsourceMapper;

    @Override
    public IPage<PhoneEntityTaskRsourceVo> pageList(Page<PhoneEntityTaskRsourceVo> page, QueryWrapper<PhoneEntityTaskRsourceVo> queryWrapper) {
        IPage<PhoneEntityTaskRsourceVo> phoneEntityTaskRsourceVoIPage = phoneEntityTaskRsourceMapper.pageList(page, queryWrapper);
        List<PhoneEntityTaskRsourceVo> records = phoneEntityTaskRsourceVoIPage.getRecords();
        records.forEach(v->{
            if(StringUtils.isNullOrEmpty(v.getLabel())){
                v.setLabelList( new ArrayList<>() );
            } else  {
                v.setLabelList( Arrays.asList( StringUtils.trim(v.getLabel()).split(",")) );
            }

        });
        phoneEntityTaskRsourceVoIPage.setRecords(records);
        return phoneEntityTaskRsourceVoIPage;
    }

    @Override
    public void resetStatus(String entityId) {
        if(!StringUtils.isNullOrEmpty(entityId)){
            phoneEntityTaskRsourceMapper.resetStatus(entityId);
        }

    }

    @Override
    public boolean checkRunning(List<String> phoneEntityTaskRsourceIdsList) {
        QueryWrapper<PhoneEntityTaskRsource> phoneEntityTaskRsourceQueryWrapper = new QueryWrapper<PhoneEntityTaskRsource>();
        phoneEntityTaskRsourceQueryWrapper
                .in("r.id",phoneEntityTaskRsourceIdsList)
                .eq("e.entity_stauts", EntityEnums.EntityStatus.RUNNING.getName());
        List<PhoneEntity> phoneEntities = phoneEntityTaskRsourceMapper.checkRunning(phoneEntityTaskRsourceQueryWrapper);

        return phoneEntities.size()>0;
    }
}
